(function(){
    /**
     * 这个js 是根据页面的标签解析得到广告位信息
     * 插入一个iframe 用于显示投放的广告的功能；   
     * _adcenter_url 参数设置 广告数据服务中心的地址，按实际部署的地址填写
     */
    var _xfadp = window._xfadp = window._xfadp || {};
    
    // _xfadp.centerurl = 'http://localhost:842/index.php?module=AdsPlace&action=showad';
    _xfadp.centerurl = 'https://tj.xfplay.com/index.php?module=AdsPlace&action=showad';
    
    // console.log(window.Xfsoltjump);
    if (window.Xfsoltjump && window.Xfsoltjump.length>0){
        //var host = window.location.host;
       
        for(var i in window.Xfsoltjump){
            var s = window.Xfsoltjump[i]['container'];
            var qid = window.Xfsoltjump[i]['id'];
            var container = document.getElementById(s);
            if (window.Xfsoltjump[i]['isloading']==1){
                 continue;
            }
            container.style.position="relative";
            var size = window.Xfsoltjump[i]['size'];
            var width = 0, height=0;
            if(size.indexOf("*")>0){
                 var wh = size.split("*");
                 width = wh[0];
                 height = wh[1];
            } else if (size.indexOf(":") > 0){
                var wh = size.split(":");
                width = window.screen.width * wh[0]/(wh[0]+wh[1]);
                height = window.screen.width * wh[1] / (wh[0] + wh[1]);
            }
            /**这里要设置富媒体 的类型 广告位位置。
             * {"html":"2222222","attachments":"http:\/\/localhost:842\/plugins\/AdsPlace\/uploads\/202201\/164247223664.png",
             * "linkurl":"222222222222","posx":"2","offsetX":"33","offsetY":"33","posy":"1",
             * "winHeight":"33","winWidth":"33",
             * "adpic":"http:\/\/localhost:842\/plugins\/AdsPlace\/uploads\/202201\/164247216374.png","type":"html"}
             */
            var eventLister = window.addEventListener || window.attachEvent;
            var msgkey = window.addEventListener ? 'message': 'onmessage';
            eventLister(msgkey, function(m){

                if (  m.data  && (typeof(m.data) == 'string') && m.data.indexOf('@@')>0 ){
                    
                    var urls = m.data.split('@@');
                     /**判断是s 相同的 子iframe 发送来的postmessage 才响应点击事件 */
                    //window.open(urls[1],'_blank');
                    if (urls.length > 1 && urls[0] == s){
                        
                        var gotolink = document.createElement('a');
                        gotolink.href = urls[1];
                        gotolink.setAttribute("target", "_blank");
                        document.body.appendChild(gotolink);
                        gotolink.click();
                        gotolink.remove(); 
                        
                    }
                }
                /**打开链接 */
                if (m && m.data
                    && m.data.container && m.data.container == s 
                    && m.data.action && m.data.action =='openurl'){
                    
                    window.open(m.data.linkurl, m.data.target);
                }
                /**不显示广告 */
                if(m && m.data                    
                     && m.data.container
                       && m.data.container == s 
                    && m.data.disabled && (m.data.disabled!=2) )
                {
                    document.getElementById(m.data.container).style.display="none";
                }
                /**富媒体的 浮窗广告类型设置样式 对应要增加 组件 preview MediasScreen 的解析 */
                if (m && m.data && m.data.container == s && m.data.type=='html' && m.data.cntype==2){
                    var md = m.data['content'];
                    container.style.position = "fixed";
                    container.style.width = md.winWidth + "px";
                    container.style.height = md.winHeight + "px";
                    if (md.posx==1){
                        container.style.left = md.offsetX+"px";
                    } else if (md.posx ==2){
                        container.style.right = md.offsetX + "px";
                    }
                    if (md.posy == 1) {
                        container.style.top = md.offsetY + "px";
                    } else if (md.posy == 2) {
                        container.style.bottom = md.offsetY + "px";
                    }                     
                }
            })
            /**设置广告位置样式 left:0;position:absolute;top:0; */
            //container.innerHTML = '<iframe src="http://localhost:842/index.php?module=AdsPlace&action=showad&idplace=15" width="400" height="300"></iframe>'
            // var url = 'https://tj.xfplay.com/index.php?module=AdsPlace&action=showad&idplace=' + qid + '" ';
            var url = _xfadp.centerurl + '&idplace=' + qid+"&cs="+s + '&referer=' + encodeURIComponent(location.href) ;
            var iframe = '<iframe id="xf' + s + i + '" name="xf' + s + i + '" '
            +' style="border:0;width:'+width+'px;height:'+height+'px;" '
            +' sandbox="allow-forms allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-top-navigation-by-user-activation" '
            + ' width="' + width + '" height="' + height +'" frameborder="0" '
            +' src="'+url+'"'
            +' marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" '
            +' data-container-id="'+s+'" data-query-id="'+qid+'" data-load-complete="true"></iframe>';
            
            container.innerHTML =iframe;
            /**显示过了设置个标识 */
            window.Xfsoltjump[i]['isloading'] = 1;
            
        }
    }
  
})();